package br.com.sisa.connection;

/**
*
* @author Marcos Godoi
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConexaoBD {
	
	private static Connection con = null;
	private static final String servidor = "localhost:";
	private static final String porta = "3306";
	private static final String banco = "/sisa";
	private static final String usuario = "root";
	private static final String senha = "";

	private static Connection conectar() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection("jdbc:mysql://" + servidor + porta + banco, usuario, senha);
			// ("jdbc:mysql://localhost:3306/crud_java", "root", "");
		} catch (Exception ex) {
			return null;
		}
	}

	public static Connection getConnection() {
		try {
			if (con == null || con.isClosed()) {
				con = conectar();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}

	public static void closeConnection(Connection conn, PreparedStatement ps) {
		try {
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs) {
		try {
			rs.close();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
